<template>
  <div class="w-full">
    <ProTable
      ref="tableRef"
      :fetchListApi="_fetchSoftwareLogCheck"
      :columns="columns"
      :hide-search="true"
      :height="500"
    >
      <template #operation="{ record }">
        <b-button type="link" @click="onLook(record)"> 查看</b-button>
        <b-button
          type="link"
          :disabled="record.clearStatus == '1'"
          @click="onStop(record)"
          code="version:upgrade:pushTotalLog:stop"
        >
          停止</b-button
        >
      </template>
    </ProTable>
  </div>
</template>

<script setup lang="tsx">
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { Modal, Switch, Tag, message } from 'ant-design-vue';
import { ProTable, ProTableProps } from '@/components/ProTable/index';
import { listToEnum } from '@/utils/utils';
import { fetchDictsOfType } from '@/api/dict';
import {
  clearFirmwareLog,
  clearSoftwareLog,
  firmPushLogV2,
  pushSoftwareLogV2,
} from '@/api/push';

const props = defineProps({
  log: {
    type: Object,
    default: () => ({}),
  },
  // target: {
  //   type: String,
  //   default: () => '',
  // },
});

// const switchLoad = ref(false);
// const loading = ref(true);
// const dataList = ref([]);
// const queryParams = ref({
//   pageNum: 1,
//   pageSize: 10,
// });
const sysOnlineStatusEnum = ref<Record<string, any>>({});
const columns = computed<ProTableProps['columns']>(() => [
  {
    title: '推送时间',
    dataIndex: 'upgradeTime',
  },
  {
    title: '推送总数',
    dataIndex: 'totalCount',
  },
  {
    title: '推送成功数',
    dataIndex: 'successCount',
  },
  {
    title: '推送失败数',
    dataIndex: 'failCount',
  },
  {
    title: '推送人',
    dataIndex: 'pushBy',
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: 120,
  },
]);
// const total = ref(0);

const tableRef = ref();
const _fetchSoftwareLogCheck = async (params: any) => {
  let res;
  if (props.log.deviceType == '1') {
    res = await pushSoftwareLogV2(props.log.versionId, {
      ...params,
      dataId: props.log.id,
    });
  } else {
    res = await firmPushLogV2(props.log.versionId, {
      ...params,
      dataId: props.log.id,
    });
  }
  return { rows: [...res.rows], total: res.total || 0 };
};

const emit = defineEmits(['closeRecord']);
const onLook = (record: any) => {
  record.deviceType = props.log.deviceType;
  record.upgradeStatus = '';
  emit('closeRecord', record);
  console.log(record);
};
const sleep = (cb) => {
  setTimeout(() => {
    cb();
  }, 500);
};
// 停止任务
const onStop = async (record: any) => {
  Modal.confirm({
    title: '系统提示',
    centered: true,
    content: `确认停止当前项？`,
    onOk: async () => {
      if (props.log.deviceType == '1') {
        try {
          await clearSoftwareLog(props.log.versionId);
          sleep(() => tableRef.value.refresh());
          message.success('停止成功');
        } catch (error) {
          console.log(error);
        }
      } else {
        try {
          await clearFirmwareLog(props.log.versionId);
          sleep(() => tableRef.value.refresh());
          message.success('停止成功');
        } catch (error) {
          console.log(error);
        }
      }
    },
  });
  let res;
};
</script>
<style lang="scss" scoped></style>
